/**
  ETFAna project, Anyang Normal University && IMP-CAS

  \class ETFHPTDCCh
  \brief the class holding the timing data acquired by the HPTDC (@CERN) chip
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-09-27
  \date 2021-10-10 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2021-09-27   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

#ifndef ETFHPTDCCh_h
#define ETFHPTDCCh_h

#include <vector>
#include "ETFRawChannel.h"

using std::vector;

class ETFHPTDCCh : public ETFRawChannel{
public:
  ETFHPTDCCh();
  ETFHPTDCCh(const ETFHPTDCCh &ch);
  virtual ~ETFHPTDCCh();

  ///@{ getters
  double time(int i = 0) const{ return GetLeadingEdge(0); }
  double GetLeadingEdge(int i = 0) const;
  double GetTrailingEdge(int i = 0) const;
  double tl(int i = 0) const{ return GetLeadingEdge(i); }
  double tt(int i = 0) const{ return GetTrailingEdge(i); }
  const vector<double> &GetLeadingEdges() const{ return vl; }
  const vector<double> &GetTrailingEdges() const{ return vt; }
  /// erase i-the element of vl or vt - i starts from 0
  int GetNofLeadingEdges() const{ return vl.size(); }
  int GetNofTrailingEdges() const{ return vt.size(); }
  int nl() const{ return vl.size(); }
  int nt() const{ return vt.size(); }
  double GetTOT(int i = 0) const;
  double tot(int i = 0) const{ return GetTOT(i); }
  bool IsVeryHighResolution() const{ return fIs_V; }
  bool IsV() const{ return fIs_V; }
  ///@}

  ///@{ setters
  virtual void AddLeadingEdgeV(int l);
  virtual void AddLeadingEdgeH(int l);
  virtual void AddTrailingEdgeV(int t);
  virtual void AddTrailingEdgeH(int t);
  virtual void SetIsVeryHighResolution(bool is_V){ fIs_V = is_V; }
  virtual void SelfCheck();
  void erase(int i, bool isLeading = true);
  ///@}

  virtual void Reset() override;
  virtual void Print(const char *opt = "") const override;

  friend class ETFPlaHit; ///< to invoke non-const version of getters for vt and vl
  friend class ETFPXIUnpacker; // to invoke AddL/Ted

  ClassDefOverride(ETFHPTDCCh, 1); // timing data acquired by HPTDC (@CERN) chip

  /// time for one channel of HPTDC HIGH resolution mode: ~100ps
  static const double kH_BLIP; //! '//!' means don't save this to file
  /// time for one channel of HPTDC VERY HIGH resolution mode: ~25ps
  static const double kV_BLIP; //!

protected:
  virtual void AddLed(double l); ///< add a leading edge
  virtual void AddTed(double t); ///< add a trailing edge
  /// \param -1: using internal bunchID
  virtual void SubtractTrigger(int bunchID); ///< intended to subtract trigger time
  vector<double> &GetLeadingEdges(){ return vl; }
  vector<double> &GetTrailingEdges(){ return vt; }
  void SetBunchID(int i){ fBunchID = i; }
  int GetBunchID() const{ return fBunchID; }

  /// \note that a channel can either be a VERY HIGH resolution HPTDC channel or
  /// a HIGH resolution HPTDC channel, but edges with mixed types in one channel
  /// is not possible
  vector<double> vl, vt;
  int fBunchID; ///< bunchID: trigger time tag
  bool fIs_V; ///< if the channel is of very high resolution or high resolution
};

#endif
